home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* menu.c - use popup menus to control animation of a robot arm
- *
- * Right Mousebutton - popup menu
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <math.h> /* for fmodf */
- #include <stdlib.h> /* for NULL */
- #include <stdio.h> /* for printf */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid menu( GLint );
-
- void printHelp( char * );
-
- /* Global Variables */
-
- static GLfloat shoulderAngle = 0.0; /* controls shoulder rotation */
-
- static GLboolean spinning = GL_TRUE;
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define MENU_SPIN 1 /* animation control option */
- #define MENU_EXIT 2 /* exit option */
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( 0, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
-
- /* create menu and add menu options */
- glutCreateMenu( menu );
- glutAddMenuEntry( "Stop Rotating Arm", MENU_SPIN );
- glutAddMenuEntry( "Exit", MENU_EXIT );
- glutAttachMenu( GLUT_RIGHT_BUTTON );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - smoothly animate a robot arm\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "Right Mousebutton - popup menu\n"
- "Escape Key - exit the program\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glShadeModel( GL_FLAT );
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 1.0, 20.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the rotation of the shoulder for each scene */
- shoulderAngle = fmodf( (shoulderAngle + 1.0), 360.0 );
-
- glutPostRedisplay(); /* Tell GLUT to redraw the scene */
- }
-
- GLvoid
- visibility( int state )
- {
- /* restart the animation function if we were
- * animated when the window was hidden
- */
- if (state == GLUT_VISIBLE && spinning) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- menu( int value )
- {
-
- switch (value) {
- case MENU_SPIN:
- spinning = !spinning;
- if (spinning) {
- glutIdleFunc( animate );
- glutChangeToMenuEntry( MENU_SPIN, "Stop Rotating Arm",
- MENU_SPIN );
- } else {
- glutIdleFunc( NULL );
- glutChangeToMenuEntry( MENU_SPIN, "Start Rotating Arm",
- MENU_SPIN );
- }
- break;
- case MENU_EXIT:
- exit(0);
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat upperArmColor[] = { 1.0, 0.0, 0.0 };
- static GLfloat lowerArmColor[] = { 0.8, 0.5, 0.5 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- gluLookAt( 0.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
- XYaxes();
-
- /* rotate the shoulder by shoulderAngle degrees */
- glRotatef( shoulderAngle, 0.0, 0.0, 1.0 );
-
- /* Draw the upper arm */
- glTranslatef( 1.0, 0.0, 0.0 );
- glColor3fv( upperArmColor );
- WireBox( 2.0, 0.4, 1.0 );
-
- /* Draw the lower arm */
- glTranslatef( 1.0, 0.0, 0.0 );
- glRotatef( 45.0, 0.0, 0.0, 1.0 );
- glTranslatef( 1.0, 0.0, 0.0 );
- glColor3fv( lowerArmColor );
- WireBox( 2.0, 0.4, 1.0 );
- glPopMatrix();
-
- glutSwapBuffers();
- }
-